#include "travelling_struct_csv.hpp"
#include "type_conversion.hpp"
#include "car.hpp"
#include "refuelling.hpp"


travelling_struct_csv::travelling_struct_csv(std::vector<std::string> const& v)
{
		
   switch(v.size()-1)
   {
   case ESTIMATED_CONSUMPTION: 
	   set_estimated_consumption(type_conversion<float>::to_number(v[ESTIMATED_CONSUMPTION]));
   case ACTIVITY_ID: 
	   set_activity_id(type_conversion<int>::to_number(v[ACTIVITY_ID]));
   case TRAVELLING_TYPE_ID:
	   set_travelling_type_id(type_conversion<int>::to_number(v[TRAVELLING_TYPE_ID]));
   case PLATE_ID:
	   set_car_id(type_conversion<int>::to_number(v[PLATE_ID]));
   case KM:
	   set_km(type_conversion<float>::to_number(v[KM]));
   case DATE:	
	   set_travelling_date(date_input_string::to_date(v[DATE]));
   case ID:
	   set_id(type_conversion<int>::to_number(v[ID]));
		break;
	default: break;
   }
		
//	std::cout << "[travelling_struct_csv.travelling_struct_csv] " << *this << std::endl;
}



float travelling_struct_csv::fuel_refund(int _plate_id)
{
	date d(this->travelling_date());
	float refund(car_csv::find_fuel_refund_unit(_plate_id, d));
	
	std::clog<< "[travelling_struct_csv.fuel_refund] unit fuel refund " << refund << " in date " << travelling_date() << std::endl;
		
	refund *= this->km();
		
	return refund;
}


float travelling_struct_csv::unit_fuel_refund(int _plate_id)
{
	date d(this->travelling_date());
	float refund(car_csv::find_fuel_refund_unit(_plate_id, d));
	
	std::clog << "[travelling_struct_csv.unit_fuel_refund] unit fuel refund " << refund << " in date " << travelling_date() << std::endl;
		
	return refund;
}

float travelling_struct_csv::fuel_consumption_physical(bool _include_last_date)
{
	float liters(this->km());

	liters*=travelling_struct_csv::estimated_fuel_consumption(_include_last_date);

	//std::cout << "[travelling_struct_csv.fuel_consumption_liters] estimated fuel consumption (LITER) = " << liters << "; in date = " << this->travelling_date() <<std::endl;
	//std::cout << "[travelling_struct_csv.fuel_consumption_liters] " << *this << std::endl;

	return liters;
}

float travelling_struct_csv::fuel_consumption_financial(bool _include_last_date)
{

	float price(this->latest_price());
	float cost(price);

	cost *= this->fuel_consumption_physical(_include_last_date);
	
	//std::cout << "[travelling_struct_csv.fuel_consumption_cost] PRICE = "<< price <<std::endl;
	//std::cout << "[travelling_struct_csv.fuel_consumption_cost] ESTIMATED COST = " << cost << std::endl;

	return cost;
		
}

float travelling_struct_csv::latest_price()
{
		
	return refuelling_csv::latest_price(this->car_id(), this->travelling_date()); // find out latest price before travelling

}

float travelling_struct_csv::estimated_fuel_consumption(bool _include_last_date)
{
	float consumption(0);

	consumption = refuelling_csv::estimated_fuel_consumption(this->car_id(), this->travelling_date(), _include_last_date);

	std::clog << "[travelling_struct_csv.estimated_fuel_consumption] estimated fuel consumption (lt/km) = " << consumption << " in date " << (this->travelling_date()) << std::endl;
	std::clog << "[travelling_struct_csv.estimated_fuel_consumption] travelling line: " << *this << std::endl;
	
	return consumption;
}
	
//[km, fuel_refund_unit, fuel_refund, estimated_fuel_consumption_liter, estimated_fuel_consumption_money, estimated_net_refund]
std::vector<float> const travelling_struct_csv::calc_travelling_parms(bool _include_last_date)
{
	std::vector<float> travelling_parms;
	
//	std::cout << this->travelling_date() << std::endl;

	travelling_parms.push_back(this->km());
//	std::cout << "[travelling_struct_csv::calc_travelling_parms] " << " km = " <<  travelling_parms[TRAVEL_KM] << std::endl;

	travelling_parms.push_back(this->unit_fuel_refund(this->car_id()));
//	std::cout << "[travelling_struct_csv::calc_travelling_parms] " << " unit fuel refund = " <<  travelling_parms[UNIT_FUEL_REFUND] << std::endl;

	travelling_parms.push_back(travelling_parms[UNIT_FUEL_REFUND] * travelling_parms[TRAVEL_KM]);
//	std::cout << "[travelling_struct_csv::calc_travelling_parms] " << " fuel refund = " <<  travelling_parms[FUEL_REFUND] << std::endl;

	travelling_parms.push_back(this->fuel_consumption_physical(_include_last_date));
//	std::cout << "[travelling_struct_csv::calc_travelling_parms] " << " estimated fuel consumption physical = " <<  travelling_parms[ESTIMATED_FUEL_CONSUMP_PHYSICAL] << std::endl;

	travelling_parms.push_back(this->fuel_consumption_financial(_include_last_date));
//	std::cout << "[travelling_struct_csv::calc_travelling_parms] " << " estimated fuel consumption financial = " <<  travelling_parms[ESTIMATED_FUEL_CONSUMP_FINANCIAL] << std::endl;

	travelling_parms.push_back(this->latest_price());
//	std::cout << "[travelling_struct_csv::calc_travelling_parms] " << " latest price = " <<  travelling_parms[LATEST_FUEL_COST] << std::endl;

	travelling_parms.push_back(travelling_parms[FUEL_REFUND] - travelling_parms[ESTIMATED_FUEL_CONSUMP_FINANCIAL]);
//	std::cout << "[travelling_struct_csv::calc_travelling_parms] " << " estimated net refund = " <<  travelling_parms[ESTIMATED_NET_REFUND] << std::endl;

	return travelling_parms;

}

